package 力扣._101_对称二叉树;

import 力扣.公用数据结构.TreeNode;

public class Solution2 {

    //交换左右子树
    void swap(TreeNode root) {
        if (root == null)
            return;
        TreeNode t = root.left;
        root.left = root.right;
        root.right = t;
        swap(root.left);
        swap(root.right);
    }

    //克隆树
    TreeNode clone(TreeNode source) {
        if (source == null)
            return null;
        TreeNode goal = new TreeNode(source.val);
        goal.left = clone(source.left);
        goal.right = clone(source.right);
        return goal;
    }
    //比较树

    boolean compare(TreeNode a, TreeNode b) {
        if (a == null && b == null)
            return true;
        if (a != null && b != null && a.val == b.val)
            return compare(a.left, b.left) && compare(a.right, b.right);
        return false;
    }

    public boolean isSymmetric(TreeNode root) {
        //生成一个左右翻转后的新树，将其和原来树比较
        if (root == null)
            return true;
        TreeNode new_tree;
        new_tree = clone(root);
        swap(new_tree);


        return compare(root, new_tree);
    }

    public static void main(String[] args) {
        TreeNode node1 = new TreeNode(1);
        TreeNode node2_1 = new TreeNode(2);
        TreeNode node2_2 = new TreeNode(2);
        TreeNode node3_1 = new TreeNode(3);
        TreeNode node3_2 = new TreeNode(3);
        node1.left=node2_1;
        node1.right=node2_2;
        node2_1.right=node3_1;
        node2_2.right=node3_2;
        boolean b = new Solution2().isSymmetric(node1);
        System.out.print(b);
    }
}
